* Most graphs following the same specification/estimation are run in loop, to search a specific figure/table from the paper search for e.g. "figure 1 panel a", "table 1 panel b", "figure s1", "table s1"

* PREAMBLE
clear all
set more off
set graphics off
capture log close
set maxvar 15000

* DIRECTORIES
cd ..
* PACKAGES
*ssc install coefplot
*ssc install reghdfe /* update if old version */
*ssc install ivreghdfe
*ssc install binscatterhist
*ssc install binscatter
*ssc install addplot
*ssc install lgraph

* DATA LOAD
use data/data_county_2019.dta

* VARIOUS GLOBALS FOR GRAPHS FORMATTING AND REGRESSIONS
if "run"=="run"{
	
* dates period
global dates_coefplot_mobility_sf "_21580 _21581 _21582 _21583 _21584 _21585 _21586 _21587 _21588 _21589 _21590 _21591 _21592 _21593 _21594 _21595 _21596 _21597 _21598 _21599 _21600 _21601 _21602 _21603 _21604 _21605 _21606 _21607 _21608 _21609 _21610 _21611 _21612 _21613 _21614 _21615 _21616 _21617 _21618 _21619 _21620 _21621 _21622 _21623 _21624 _21625 _21626 _21627 _21628 _21629 _21630 _21631 _21632 _21633 _21634 _21635 _21636 _21637 _21638 _21639 _21640 _21641 _21642 _21643 _21644 _21645 _21646 _21647 _21648 _21649 _21650 _21651 _21652 _21653 _21654 _21655 _21656 _21657 _21658 _21659 _21660 _21661 _21662 _21663 _21664 _21665 _21666 _21667 _21668 _21669"
global x_line_sf "xline(0.511 0.665 0.830 0.983 1.170 1.324 1.489, lpattern(dot) lwidth(1) lcolor(gray)) xlabel(0.511 "February 1st" 0.665 "February 15th" 0.830 "March 1st" 0.983 "March 15th" 1.170 "April 1st" 1.324 "April 15th" 1.489 "April 30th")"


* first stage
global general_options_fs_fxnc "graphregion(fcolor(white))  legend(off) ytitle("Fox News Viewership") xtitle("Fox News Channel Position (Standardized)")  color(teal) lcolor(maroon) "
global general_options_fs_cnn "graphregion(fcolor(white))  legend(off) ytitle("CNN Viewership") xtitle("CNN Position (Standardized)")  color(teal) lcolor(maroon) "
global general_options_fs_msnbc "graphregion(fcolor(white))  legend(off) ytitle("MSNBC Viewership") xtitle("MSNBC Position (Standardized)")  color(teal) lcolor(maroon) "

* reduced form
global general_options_reduced "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on Mobility (RF)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) xtitle("Date") ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global general_options_reduced_p "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on Purchases (RF)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) xtitle("Date") ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global general_options_reduced_ratioc "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on Infection Rate (RF)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) xtitle("Date") ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global general_options_reduced_ratiod"graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on Deaths Per Capita (RF)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) xtitle("Date") ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global other_options_reduced " graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on `y' (RF)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"

* OLS
global general_options_ols "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on Mobility (OLS)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) xtitle("Date") ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global general_options_ols_p "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on Purchases (OLS)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) xtitle("Date") ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global general_options_ols_ratioc "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on Infection Rate (OLS)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) xtitle("Date") ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global general_options_ols_ratiod "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on Deaths Per Capita (OLS)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) xtitle("Date") ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global other_options_ols "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on `y' (OLS)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"

* 2SLS
global general_options "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on Mobility (2SLS)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) xtitle("Date") ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global general_options_p "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on Purchases (2SLS)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) xtitle("Date") ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global general_options_ratioc "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on Infection Rate (2SLS)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) xtitle("Date") ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global general_options_ratiod "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on Deaths Per Capita (2SLS)") yline(0, lpattern(dash) lcolor(gray)) xsize(8) xtitle("Date") ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global other_options_iv "graphregion(margin(3 6 3 3))  graphregion(fcolor(white)) vertical legend(off) ytitle("Effect on `y' (2SLS)") yline(0, lpattern(dash) lcolor(gray)) xsize(8)  ciopts(color(teal)) msize(small) symbol(circle_hollow) color(maroon)"
global x_line "xline(0.518  1.482, lpattern(dot) lcolor(gray)) xlabel(0.518 "March 17th"  1.482 "May 10th")"

* globals for other networks controls
global other_rtgs_fxnc "msnbc_rtg cnn_rtg"
global other_rtgs_cnn "msnbc_rtg fxnc_rtg"
global other_rtgs_msnbc "fxnc_rtg cnn_rtg"

global other_ch_fxnc "msnbc cnn"
global other_ch_cnn "msnbc fxnc"
global other_ch_msnbc "fxnc cnn"


global outcomes_pick "sd_sf_dist_fr_home sd_sf_median_nohome_time"
global outcomes_pick1 "ratio_cases_pop ratio_deaths_pop st_incr_cases_7_ st_incr_deaths_7_"
global outcomes_pick4 "tot_expgroc"

* controls
global full_controls1 " pop_dens_county landarea_county age_imb pct_food_stamps_county med_rep16 med_rep12 pct_nohs_county pct_college_county med_white med_black pct_male_county"

}
*

* FIRST STAGE CHECK
preserve
foreach network in fxnc  {
replace `network'=-`network'
ivreghdfe sd_sf_dist_fr_home21551 (`network'_rtg=`network') [aweight=pop], absorb(state_fips one##c.($full_controls1 ${other_rtgs_`network'})) cluster(state_fips) first
binscatter 	`network'_rtg `network' [aweight=pop], absorb(state_fips) controls($full_controls1 ${other_rtgs_`network'}) nquantiles(19) ${general_options_fs_`network'}
graph export "output/2019_firststage_`network'_censuscontrols1_state_fips.pdf", replace
}
restore

* SAFEGRAPH MOBILITY (with baseline, 2sls) 
* figure s15
foreach cl in state_fips {
	foreach network in fxnc {
	* Main outcomes IV
			foreach y of global outcomes_pick {
				cap drop _est*
				eststo clear
					forvalues date= 21580/21669 {
					 ivreghdfe `y'`date' (`network'_rtg= `network') [aweight=pop], absorb(state_fips one##c.($full_controls1 ${other_rtgs_`network'})) cluster(`cl')
					cap eststo _`date'
					}
				macro drop options					
				coefplot $dates_coefplot_mobility_sf, $general_options $x_line_sf keep(`network'_rtg) 
				graph export "output/2019_2016_`network'_censuscontrols1_`cl'_`y'.pdf", replace
				}
	*
	}
}
*				
				
